<template>
	<view class="wo-page" v-show="pageInit">
		<!--操作区域-->
		<view class="wo-action">
		    <u-row gutter="20" justify="center">
		        <u-col span="6"><u-button type="success" shape="circle" @tap="userFunc">用户注册登陆</u-button></u-col>
		    </u-row>
		</view>
		<!--登陆提示-->
		<u-modal v-model="loginModalShow" :content="loginModalContent" @confirm="loginFunc"></u-modal>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 显示登陆提示
				loginModalShow: false,
				// 登陆提示内容
				loginModalContent: '请授权登陆',
			};
		},
		methods: {
			/**
			 * 页面初始化后执行
			 * @param {Object} option
			 */
			initPageFunc(option) {
			    this.pageInit = true;
			},
			/**
			 * 用户注册
			 */
			async userFunc() {
			    // #ifdef H5
			    if (this.clientType == 'wechat_mobile' || this.clientType == 'wechat_computer') {
			        console.log('start-->startFunc[vuex_wx_token]:', this.vuex_wx_token);
			        if (!this.vuex_wx_token) {
			            this.loginModelContent = '请授权微信登陆';
			            this.loginModalShow = true;
			            return false;
			        }
			    }
			    // #endif
			    if (!this.vuex_user_info || !Object.keys(this.vuex_user_info).length) {
			        // 拉取用户信息
			        let userInfo = await this.$u.api.userInfo();
			        console.log('start-->startFunc[userInfo]:', userInfo);
			        if (!userInfo) {
			            return false;
			        }
			        this.$u.vuex('vuex_user_info', userInfo);
			    }
			    // 路由到首页
			    this.$u.route({
			        url: 'pages/index'
			    });
			},
			/**
			 * 登陆方法
			 */
			loginFunc() {
			    // #ifdef H5
			    if (this.clientType == 'wechat_mobile' || this.clientType == 'wechat_computer') {
			        let loginInfo = this.$wx.loginFunc(this.$config.wx_id, this.$config.wx_login_link, this.$config.wx_login_scope, this.$config.wx_success_redirect, this.$config.wx_error_redirect);
			        if (!loginInfo) {
			            /*
			            // 跳到错误页
			            this.$u.route({
			                type: 'reLaunch',
			                url: 'pages/error',
			                params: {
			                    message: '授权登陆失败'
			                }
			            });
			            */
			            return false;
			        }
			        this.$u.vuex('vuex_wx_id', loginInfo.id);
			        this.$u.vuex('vuex_wx_token', loginInfo.token);
			        return true;
			    }
			    // #endif
			},
			
		}
	}
</script>

<style lang="scss">
	.wo-action {
	    position: fixed;
	    bottom: uni-unit(100px);
	    width: 100%;
	}
</style>
